package com.feicheng.lanniao.service.impl;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feicheng.lanniao.entity.CarBuyClientVo;
import com.feicheng.lanniao.entity.HouseArea;
import com.feicheng.lanniao.entity.HouseSellClientVo;
import com.feicheng.lanniao.entity.HouseSellPrice;
import com.feicheng.lanniao.mapper.HouseSellClientVoMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.IHouseAreaService;
import com.feicheng.lanniao.service.IHouseSellClientVoService;
import com.feicheng.lanniao.service.IHouseSellPriceService;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-07
 */
@Service
public class HouseSellClientVoServiceImpl extends ServiceImpl<HouseSellClientVoMapper, HouseSellClientVo>
		implements IHouseSellClientVoService {

	@Autowired
	private IHouseSellPriceService iHouseSellPriceService;
	@Autowired
	private IHouseAreaService iHouseAreaService;

	// 1--卖房列表查询（判断+当前页+每页多少条）
	@Override
	public ResultInfo getList(String des,Integer provienceId,
										   Integer cityId, Integer reignId,
										   Integer clientId, Integer sellId,
										   String typeName, Integer houseAreaId, Integer sellPriceId,
										   String tel,
										   String wxNumber,
										   String louceng,
										   String xiaoqu,
										   String pianqu,
										   String chaoxiang,
										   String zhuangxiu,
										   Integer dianti,
										   String niandai,
										   Integer roomCount,
										   Integer currentPage,
										   Integer pageSize) {
		System.out.println("+++++++++++++"+ currentPage);
		
		QueryWrapper<HouseSellClientVo> queryWrapper = new QueryWrapper<HouseSellClientVo>();
		queryWrapper.eq("sale_flg", "1");
		// -----------判断描述不为空的情况下---------- 按描述搜索
		if (StringUtils.hasLength(des)) {
			queryWrapper.like("des", des);
		}
		// ----------- 判断面积区间id不为空的情况下----------- 按面积区间检索
		if (null != houseAreaId && 0 != houseAreaId) {
			// 得到1个面积区间对象
			HouseArea houseArea = iHouseAreaService.getById(houseAreaId);
			// 在条件中做检索 在面积区间的最小-最大之间做查询
			queryWrapper.between("area", houseArea.getMinArea(), houseArea.getMaxArea());
			// queryWrapper.eq("area", area);//按面积搜索
		}
		// --------------判断价格区间id不为空且不为0-----------按照价格区间检索
		if (null != sellPriceId && 0 != sellPriceId) {
			HouseSellPrice houseSellPrice = iHouseSellPriceService.getById(sellPriceId);
			queryWrapper.between("total_Price", houseSellPrice.getMinPrice(), houseSellPrice.getMaxPrice());
		}
		queryWrapper.eq("del_flg", "1");

		//----------------判断片区是否为空-------
		if(pianqu != null && pianqu !=""){
			queryWrapper.like("pianqu",pianqu);
		}

		if(xiaoqu != null && xiaoqu != ""){
			queryWrapper.like("xiaoqu",xiaoqu);
		}
		if(null != dianti && 0 != dianti){
			queryWrapper.eq("dianti",dianti);
		}

		if(zhuangxiu != null && zhuangxiu != ""){
			queryWrapper.like("zhuangxiu", zhuangxiu);
		}

		if(provienceId != null && 0 != provienceId){
			System.out.println("_____________________" + provienceId);
			queryWrapper.eq("provience_id", provienceId);
		}

		if(cityId != null && 0 != cityId){
			queryWrapper.eq("city_id", cityId);
		}

		if(null != reignId  && reignId != 0){
			queryWrapper.eq("reign_id", reignId);
		}

		if(typeName != null && typeName != ""){
			queryWrapper.like("type_name", typeName);
		}

		if(roomCount != null){
			queryWrapper.eq("room_count", roomCount);
		}
		if(louceng != null){
			queryWrapper.like("louceng", louceng);
		}
		
		if(chaoxiang != null){
			queryWrapper.like("chaoxiang", chaoxiang);
		}
		if (clientId != null && clientId != 0) {
			queryWrapper.eq("client_id",clientId);
		}
		IPage<HouseSellClientVo> pageInfo = new Page<HouseSellClientVo>(currentPage, pageSize);
		return ResultInfo.success(this.page(pageInfo, queryWrapper));
	}

	@Override
	public ResultInfo getList(String des, Integer provienceId, Integer cityId, Integer reignId, Integer clientId,
			Integer sellId, String typeName, Integer houseAreaId, Integer sellPriceId, String tel, String wxNumber,
			String louceng, String xiaoqu, String pianqu, String chaoxiang, String zhuangxiu, Integer dianti,
			String niandai, Integer roomCount, Integer currentPage, Integer pageSize, String sort, Boolean dec) {
		// TODO Auto-generated method stub
		QueryWrapper<HouseSellClientVo> queryWrapper = new QueryWrapper<HouseSellClientVo>();
		queryWrapper.eq("sale_flg", "1");
		// -----------判断描述不为空的情况下---------- 按描述搜索
		if (StringUtils.hasLength(des)) {
			queryWrapper.like("des", des);
		}
		// ----------- 判断面积区间id不为空的情况下----------- 按面积区间检索
		if ( null != houseAreaId && 0 != houseAreaId) {
			// 得到1个面积区间对象
			HouseArea houseArea = iHouseAreaService.getById(houseAreaId);
			// 在条件中做检索 在面积区间的最小-最大之间做查询
			queryWrapper.between("area", houseArea.getMinArea(), houseArea.getMaxArea());
			// queryWrapper.eq("area", area);//按面积搜索
		}
		// --------------判断价格区间id不为空且不为0-----------按照价格区间检索
		if (null != sellPriceId && 0 != sellPriceId) {
			HouseSellPrice houseSellPrice = iHouseSellPriceService.getById(sellPriceId);
			queryWrapper.between("total_Price", houseSellPrice.getMinPrice(), houseSellPrice.getMaxPrice());
		}
		queryWrapper.eq("del_flg", "1");

		//----------------判断片区是否为空-------
		if(pianqu != null && pianqu !=""){
			queryWrapper.like("pianqu",pianqu);
		}
		if(null != dianti && 0 != dianti){
			queryWrapper.eq("dianti",dianti);
		}

		if(xiaoqu != null && xiaoqu != ""){
			queryWrapper.like("xiaoqu",xiaoqu);
		}

		if(zhuangxiu != null && zhuangxiu != ""){
			queryWrapper.like("zhuangxiu", zhuangxiu);
		}

		if(provienceId != null && provienceId != 0){
			System.out.println("_____________________" + provienceId);
			queryWrapper.eq("provience_id", provienceId);
		}

		if(cityId != null && cityId != 0){
			queryWrapper.eq("city_id", cityId);
		}

		if(reignId != null && reignId != 0){
			queryWrapper.eq("reign_id", reignId);
		}

		if(typeName != null && typeName != ""){
			queryWrapper.like("type_name", typeName);
		}

		if(roomCount != null){
			queryWrapper.eq("room_count", roomCount);
		}
		if(louceng != null){
			queryWrapper.like("louceng", louceng);
		}
		if(chaoxiang != null){
			queryWrapper.like("chaoxiang", chaoxiang);
		}
		if (clientId != null && clientId != 0) {
			queryWrapper.eq("client_id",clientId);
		}
		//判断排序条件order是否为空
		if(StringUtils.hasLength(sort)) {
			if (!sort.equals("default")) {
				if(dec) {
					queryWrapper.orderByDesc(sort);
				}
				else {
					queryWrapper.orderByAsc(sort);
				}
			}
			else {
				queryWrapper.orderByAsc("sell_id");
			}
		}
		IPage<HouseSellClientVo> pageInfo = new Page<HouseSellClientVo>(currentPage, pageSize);
		return ResultInfo.success(this.page(pageInfo, queryWrapper));
	}
}
